package com.wisdytech.linkdcs.partLife.action;


import com.wisdytech.common.handler.ResourceMatchingService;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import com.wisdytech.linkdcs.partLife.vo.PartVO;
import com.wisdytech.linkdcs.system.model.SysUser;
import com.wisdytech.linkdcs.partLife.model.Part;
import com.wisdytech.linkdcs.partLife.service.IPartService;

/**
 * <p>
 *  备件信息前端控制器
 * </p>
 *
 * @author chenbin
 * @since 2018-06-05
 */
@Controller
@RequestMapping("/part/info")
public class PartController {

    /**
     * 注入备件信息iPartService
     */
    @Autowired
    @Qualifier("iPartService")
    private IPartService iPartService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "lifeCycle/partLifeCycle/partInfo";
    }
    
    /**
     * 分页条件查询备件列表信息
     * @param partCode 备件编码
     * @param partName 备件名称
     * @param modelCode 设备型号
     * @param partType 备件类型
     * @param quickWearFlag 易损件
     * @param keyPartFlag 关键备件
     * @param filter 分页参数
     * @return 备件管理列表信息
     * @throws IOException 
     */
    @RequestMapping(value = "/listPartByFilter/get.mvc")
    @ResponseBody	
    public StandardTableResult<PartVO> listPartByFilter(String partCode, String partName,String modelCode, 
												    		String partType,String quickWearFlag,
												    		String keyPartFlag,String partSpecification,Filter filter) throws IOException{
    	PageInfo<Part> parts =this.iPartService.listPartByFilter(partCode, partName,modelCode,partType,quickWearFlag,keyPartFlag, partSpecification,filter);
        //转换为前台结果集
        List<Part> partList=parts.getList();
        List<PartVO> partVOList=new ArrayList<PartVO>();
        for(Part part:partList){
        	partVOList.add(DataTransformUtil.changePartVO(part));
        }
        return new StandardTableResult<PartVO>(parts.getTotal(),partVOList);
    }
    
    /**
     * 保存备件信息
     * @param part 备件对象
     * @param session 
     * @return 保存结果集
     */
    @RequestMapping(value = "/savePart/save.mvc")
    @ResponseBody
    public StandardResult savePart(Part part, HttpSession session) {
        SysUser sysUser = BaseUtil.getCurrentUser();
        String loginName = sysUser.getUsername();
        if (StringUtils.isBlank(part.getPkId())) {
        	iPartService.addPart(part, loginName);
        }else {
        	iPartService.updatePart(part, loginName);
        }
        return  StandardResult.getSuccessResult();
    }
    
    /**
     * 通过id批量删除备件信息
     * @param id 主键id
     * @return 删除结果集
     */
    @RequestMapping(value = "/delPartById/del.mvc")
    @ResponseBody
    public StandardResult delFactoryById(String pkId) {
    	iPartService.delPartById(pkId);
        return StandardResult.getSuccessResult();
    }
    
    /**
     * <p>Description:查询备件信息</p> 
     * @param pkId 备件id
     * @return 备件信息
     * @throws IOException 
     */
    @RequestMapping(value = "/getPartById/get.mvc")
    @ResponseBody
    public StandardResult getPartById(String pkId){
    	Part part = iPartService.getPartById(pkId);
        return StandardResult.getSuccessResult(part);
    }
    
    /**
     * <p>Description:查询关键备件编号信息列表</p> 
     * @param search 模糊查询
     * @return 关键备件编号信息列表集合
     */
    @RequestMapping(value = "/listKeyPartAll/get.mvc")
    @ResponseBody
    public List<Part> listKeyPartAll(@ModelAttribute("search") String search){
        return iPartService.listKeyPartAll(search);
    }
    
    /**
     * <p>Description:查询与设备型号关联的专用件和未和设备关联的通用件信息列表</p> 
     * @param search 模糊查询
     * @param modelCode 设备型号
     * @param filter 分页查询
     * @return 查询与设备型号关联的专用件和未和设备关联的通用件信息列表
     */
    @RequestMapping(value = "/listModelPartAll/get.mvc")
    @ResponseBody
    public StandardResult listModelPartAll(@ModelAttribute("search") String search,String modelCode,Filter filter){
    	List<Part> list = iPartService.listModelPartAll(search,modelCode,filter);
        return  StandardResult.getSuccessResult(list);
    }
    
    /**
     * 通过备件编号查询对应的备件信息
     * @param partCode 备件编码
     * @return 备件信息对象
     */
    @RequestMapping(value = "/getPartByPartCode/get.mvc")
    @ResponseBody
    public StandardResult getPartByPartCode(String partCode){
        Part part = iPartService.getPartByPartCode(partCode);
        PartVO partVO = new PartVO();
        if (part != null) {
        	partVO = DataTransformUtil.changePartVO(part);
        }
        return StandardResult.getSuccessResult(partVO);
    }
    
    /**
     * <p>Description:查询所有备件</p> 
     * @param search 模糊查询
     * @param filter 分页查询
     * @return 查询结果集
     */
    @RequestMapping(value = "/listlPartAllByPartCode/get.mvc")
    @ResponseBody
    public StandardResult listlPartAllByPartCode(@ModelAttribute("search") String search,Filter filter){
    	List<Part> list = iPartService.listlPartAllByPartCode(search,filter);
        return  StandardResult.getSuccessResult(list);
    }
}

